package com.njbdqn.ALi.zjg.dao;

import java.util.List;

import org.njbdqn.ali.database.SqlTemplate;

import com.njbdqn.ALi.zjg.entity.Comment;
import com.njbdqn.ALi.zjg.entity.PageHelper;
import com.njbdqn.ALi.zjg.vo.CommentVO;

public class CommentDAO {
	private SqlTemplate template;
	private PageHelper helper;
	
	public CommentDAO(){
		template = new SqlTemplate();
		helper = new PageHelper();
	}
	public int save(Comment comment){	
		String sql="insert into comment (account,book_id,replyid,commentcontent,commenttime,great, "
				+ " commentnumber) values (?,?,?,?,?,?,?)";
		return template.update(sql, new Object[]{comment.getAccount(),comment.getBook_id(),
				comment.getReplyid(),comment.getCommentcontent(),comment.getCommenttime(),
				comment.getGreat(),comment.getCommentnumber()});
	}
	public List<CommentVO> findAll(int book_id){
		String sql="select c.commentid,u.userimg,u.nickname,c.commentcontent,c.commenttime from "
				+ " userinfos u, comment c where c.account=u.account  and replyid=0 and c.book_id=? limit 0,10";
		return template.resultToArray(sql, new Object[]{book_id}, CommentVO.class);
	}
	
	public List<CommentVO> findAllSelect(int cp,int pageSize,int book_id){
		helper.setPage(cp);
		helper.setSize(pageSize);
		String sql="select c.commentid,u.userimg,u.nickname,c.commentcontent,c.commenttime from "
				+ " userinfos u, comment c where c.account=u.account and replyid=0 and c.book_id=?";
		return helper.getBean(sql, new Object[]{book_id}, CommentVO.class).getRows();
	}
	
	public int findNumber(int commentid){
		String sql="select count(commentid) from comment where replyid=?";
		return Integer.parseInt(template.resultToObject(sql, new Object[]{commentid}).toString());
	}
	public int findCommentNumber(int book_id){
		String sql="select count(commentid) from comment where book_id=?";
		return Integer.parseInt(template.resultToObject(sql, new Object[]{book_id}).toString());
	}
}